<template>
  <a-modal
    :title="title"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="当前折扣比例"
          hasFeedback >
          <a-input placeholder="请输入折扣比例" v-model="discountRatio" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="折扣比例规则"
          hasFeedback required>
          <span style="font-size: 12px;color: #2d2d2d;">：不打折的折扣比例是1，默认不打折，打9折请输入0.9，最多支持小数位后两位</span>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  export default {
    name: "shezhizhekou",
    data () {
      return {
        discountId:'',
        id:'',
        discountRatio:'',
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          add: "/Discount/shezhizhekou",
          edit: "/Discount/shezhizhekou",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record,discountId) {
        this.discountId=discountId;
           this.id=record.id;
        this.discountRatio=record.discountRatio;
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        // this.$nextTick(() => {
        //   this.form.setFieldsValue(pick(this.model,'name','keyWord','sex','age','email','content'))
		    //   //时间格式化
        //   this.form.setFieldsValue({punchTime:this.model.punchTime?moment(this.model.punchTime,'YYYY-MM-DD HH:mm:ss'):null})
        //   this.form.setFieldsValue({birthday:this.model.birthday?moment(this.model.birthday):null})
        // });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
            if(this.discountRatio==null || this.discountRatio==''){
              this.$message.warning("请输入具体折扣比例");
              return;
            }else if(!reg.test(this.discountRatio)){
              this.$message.warning("请输入正确的折扣比例")
              return;
            }
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'post';
            }
            let formData = Object.assign(this.model, values);
           formData['discountRatio']=this.discountRatio;
           formData['id']=this.id;
           formData['discountId']=this.discountId;
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.$emit('aaa');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style scoped>

</style>